package com.doctor.ui;

import com.doctor.comm.ConstConfig;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebViewClient;


public class WebsiteActivity extends BaseActivity{

	/**
	 * 网站
	 */
	
	private WebView webView;
	private String url;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.website);
		initiaView();
		
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		webView=(WebView) findViewById(R.id.webView);
		WebSettings settings=webView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setPluginState(PluginState.ON);
		settings.setBuiltInZoomControls(true);
		url=getIntent().getStringExtra(ConstConfig.URL);
		webView.loadUrl(url);
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				//防止回退重新加载
				return super.shouldOverrideUrlLoading(view, url);
			}
			
			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				// TODO Auto-generated method stub
				super.onReceivedSslError(view, handler, error);
				handler.proceed();
			}
			
			@Override
			public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
				// TODO Auto-generated method stub
				return super.shouldOverrideKeyEvent(view, event);
			}
			
		});
		
		//设置弹出网页窗口
		webView.setWebChromeClient(new WebChromeClient(){
			@Override
			public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(WebsiteActivity.this)
				.setMessage(message)
				.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {			
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						result.confirm();
					}
				})
				.create().show();
				return true;
			}
		});
		
		webView.setOnKeyListener(new OnKeyListener() {			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction()==KeyEvent.ACTION_DOWN){
					if (webView.canGoBack() && event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
						webView.goBack();
						return true;
					}
				}		
				return false;		
			}
		});
		
	}
	
	
	
	
	
}
